﻿$(document).ready(function () {

    btn_user_detail();
    btn_user_delete();

    btn_user_add();
    btn_user_change();


    btn_record_delete();
});

function btn_user_detail() {
    $(".btn_user_detail").die().live("click", function (e) {
        e.preventDefault();
        var thishandle = $(this);
        var userid = $(this).parent().parent().find(".text_gift_id").text();
        $.trim(userid);

        var reg = new RegExp("(^|&)" + "username" + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        var username = null;
        if (r != null)
            username = unescape(r[2]);

        window.location.href = "/User/User_Detail?username=" + username + "&_user_id=" + userid;
    });
}


function btn_user_delete() {
    $(".btn_user_delete").die().live("click", function (e) {
        e.preventDefault();
        var thishandle = $(this);
        var _user_id = $(this).parent().parent().find(".text_gift_id").text();
        $.ajax({
            url: "/UserAction/User_Delete",
            type: "post",
            cache: false,
            data: {
                _user_id: _user_id
            },
            dataType: "json",
            success: function (msg) {
                if (msg.Result) {
                    thishandle.parent().parent().css("opacity", "0");
                    thishandle.parent().parent().delay(200).queue(function () {
                        $(this).remove();
                    });

                } else {
                    alert("删除失败");
                }
            }
        });
    });
}

function btn_user_add() {
    $("#btn_user_add").die().live("click", function (e) {
        e.preventDefault();
        var _user_id = $("#text_user_userid").val();
        var _user_name = $("#text_user_username").val();
        var _user_psw = $("#text_user_userpsw").val();
        var _user_phone = $("#text_user_userphone").val();
        var _user_phone = $("#text_user_userphone").val();
        var _user_credit = $("#text_user_usercredit").val();
        var _user_level = $("#text_user_userlevel").val();
        //        var str = _user_id + _user_name + _user_psw + _user_phone + _user_credit + _user_level;
        //        alert(str);
        if (_user_id == "") {
            alert("用户id不能为空");
            return;
        }
        if (_user_name == "") {
            alert("用户名不能为空");
            return;
        }
        if (_user_psw == "") {
            alert("用户密码不能为空");
            return;
        }
        if (_user_phone == "") {
            alert("用户电话不能为空");
            return;
        }
        if (_user_credit == "") {
            alert("用户积分不能为空");
            return;
        }
        if (_user_level == "") {
            alert("用户等级不能为空");
            return;
        }
        $.ajax({
            url: "/UserAction/User_Add",
            type: "post",
            cache: false,
            data: {
                _user_id: _user_id,
                _user_name: _user_name,
                _user_psw: _user_psw,
                _user_phone: _user_phone,
                _user_credit: _user_credit,
                _user_level: _user_level
            },
            dataType: "json",
            success: function (msg) {
                if (msg.Result) {
                    alert("添加成功");
                } else {
                    alert("添加失败");
                }
            }
        });
    });

}

function btn_user_change() {
    $("#btn_user_change").die().live("click", function (e) {
        e.preventDefault();
        var _user_id = $("#text_user_userid").val();
        var _user_name = $("#text_user_username").val();
        var _user_psw = $("#text_user_userpsw").val();
        var _user_phone = $("#text_user_userphone").val();
        var _user_credit = $("#text_user_usercredit").val();
        var _user_level = $("#text_user_userlevel").val();

        $.trim(_user_id);
        $.trim(_user_name);
        $.trim(_user_psw);
        $.trim(_user_phone);
        $.trim(_user_credit);
        $.trim(_user_level);
        //        var str = _user_id + _user_name + _user_psw + _user_phone + _user_credit + _user_level;
        //        alert(str);
        if (_user_id == "") {
            alert("用户id不能为空");
            return;
        }
        if (_user_name == "") {
            alert("用户名不能为空");
            return;
        }
        if (_user_psw == "") {
            alert("用户密码不能为空");
            return;
        }
        if (_user_phone == "") {
            alert("用户电话不能为空");
            return;
        }
        if (_user_credit == "") {
            alert("用户积分不能为空");
            return;
        }
        if (_user_level == "") {
            alert("用户等级不能为空");
            return;
        }
        $.ajax({
            url: "/UserAction/User_Change",
            type: "post",
            cache: false,
            data: {
                _user_id: _user_id,
                _user_name: _user_name,
                _user_psw: _user_psw,
                _user_phone: _user_phone,
                _user_credit: _user_credit,
                _user_level: _user_level
            },
            dataType: "json",
            success: function (msg) {
                if (msg.Result) {
                    alert("修改成功");
                } else {
                    alert("修改失败");
                }
            }
        });
    });
}


function btn_record_delete() {
    $(".btn_record_delete").die().live("click", function (e) {
        e.preventDefault();
        var thishandle = $(this);
        var _user_id = $(this).parent().parent().find(".text_gift_id").text();
        alert(_user_id);
        $.ajax({
            url: "/DataRecord/Gift_Delete",
            type: "post",
            cache: false,
            data: {
                _user_id: _user_id
            },
            dataType: "json",
            success: function (msg) {
                if (msg.Result) {
                    thishandle.parent().parent().css("opacity", "0");
                    thishandle.parent().parent().delay(200).queue(function () {
                        $(this).remove();
                    });

                } else {
                    alert("删除失败");
                }
            }
        });
    });
}